package com.songshan.dto;

import com.songshan.exception.BusinessException;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * @author feb
 * @date 2024/6/18
 * @Description: 接口响应结果
 */
public class Result {

  private Integer code;
  private String msg;
  private String token;
  private List<FieldError> error;
  private Object data;

  public static Result success() {
    Result result = new Result();
    result.setCode(200);
    return result;
  }

  public static Result success(Object data) {
    Result result = new Result();
    result.setCode(200);
    result.setData(data);
    return result;
  }

  public static Result failure(BusinessException exception) {
    Result result = new Result();
    result.setCode(exception.getCode());
    result.setMsg(exception.getMessage());
    return result;
  }

  public static Result failure(List<FieldError> error) {
    Result result = new Result();
    result.setCode(500);
    result.setError(error);
    return result;
  }

  public Object getData() {
    return data;
  }

  public void setData(Object data) {
    this.data = data;
  }

  public Integer getCode() {
    return code;
  }

  public void setCode(Integer code) {
    this.code = code;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public String getToken() {
    return token;
  }

  public void setToken(String token) {
    this.token = token;
  }

  public List<FieldError> getError() {
    return error;
  }

  public void setError(List<FieldError> error) {
    this.error = error;
  }
}
